package com.fsai.cms.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fsai.cms.base.Ztree;
import com.fsai.cms.domain.CmsCate;
import com.fsai.cms.mapper.CmsCateMapper;
import com.fsai.cms.mapper.ISqlMapper;
import com.fsai.cms.service.ICmsCateService;

import cn.hutool.core.collection.CollUtil;

/**
 * 栏目设置Service业务层处理
 * 
 * @author jaffe
 * @date 2021-03-12
 */
@Service
public class CmsCateServiceImpl implements ICmsCateService {
	@Autowired
	private CmsCateMapper cmsCateMapper;

	@Autowired
	private ISqlMapper sqlMapper;
	//	@Autowired
	//	private IBaseDao<CmsCate> baseDao;

	/**
	 * 查询栏目设置树列表
	 * 
	 * @return 所有栏目设置信息
	 */
	@Override
	public List<Ztree> selectCmsCateTree(String orgId) {
		CmsCate queryParam = new CmsCate();
		queryParam.setOrgId(Long.parseLong(orgId));
		queryParam.setStatus("0");
		List<CmsCate> cmsCateList = cmsCateMapper.selectCmsCateList(queryParam);
		List<Ztree> ztrees = new ArrayList<Ztree>();
		for (CmsCate cmsCate : cmsCateList) {
			Ztree ztree = new Ztree();
			ztree.setId(cmsCate.getCateId());
			ztree.setpId(cmsCate.getParentId());
			ztree.setName(cmsCate.getCateTitle());
			ztree.setTitle(cmsCate.getCateTitle());
			ztrees.add(ztree);
		}
		return ztrees;
	}

	@Override
	public List<CmsCate> selectCateList(String orgId) {
		CmsCate queryParam = new CmsCate();
		queryParam.setOrgId(Long.parseLong(orgId));
		queryParam.setStatus("0");

		List<CmsCate> cmsCateList = cmsCateMapper.selectCmsCateList(queryParam);
		//		List<CmsCate> findList = baseDao.findList("BizSql.selectCateNavList", BeanMapTool.beanToMap(queryParam));

		return cmsCateList;
	}

	@Override
	public List<CmsCate> selectCateListToTree(String orgCode, int topCateId) {
		//0查询机构id
		String sql = "SELECT org_id from base_org where org_code='" + orgCode + "' ";
		String orgId = sqlMapper.selectOneProp(sql);
		//1查询导航栏目
		CmsCate queryParam = new CmsCate();
		queryParam.setOrgId(Long.parseLong(orgId));
		queryParam.setStatus("0");
		List<CmsCate> cateList = cmsCateMapper.selectCmsCateList(queryParam);

		//转换为tree-父子模式
		List<CmsCate> cateTreeList = new ArrayList<>();
		for (CmsCate cmsCate : cateList) {
			if (cmsCate.getParentId() == null || cmsCate.getParentId() == topCateId) {
				cateTreeList.add(cmsCate);
			}
		}
		findChildren(cateTreeList, cateList);

		//list排序
		if (!cateTreeList.isEmpty()) {
			CollUtil.sortByProperty(cateTreeList, "sortIndex");
		}
		return cateTreeList;
	}

	private void findChildren(List<CmsCate> cateTreeList, List<CmsCate> cateList) {
		for (CmsCate cmsCate : cateTreeList) {
			List<CmsCate> childern = new ArrayList<>();
			for (CmsCate cmsCate2 : cateList) {
				if (cmsCate.getCateId().equals(cmsCate2.getParentId())) {
					childern.add(cmsCate2);
				}
			}
			cmsCate.setChildren(childern);
			findChildren(childern, cateList);
		}
	}

	@Override
	public CmsCate queryCateByCode(String cateCode) {
		CmsCate query = new CmsCate();
		query.setCateCode(cateCode);

		return cmsCateMapper.selectCateByParam(query);
	}

	/**
	 * 查询栏目设置
	 * 
	 * @param cateId
	 *            栏目设置ID
	 * @return 栏目设置
	 */
	@Override
	public CmsCate selectCmsCateById(Long cateId) {
		return cmsCateMapper.selectCmsCateById(cateId);
	}

	@Override
	public CmsCate querySubCate(Long parentCateId, String cateTitle) {
		CmsCate query = new CmsCate();
		query.setCateTitle(cateTitle);
		query.setParentId(parentCateId);

		return cmsCateMapper.selectCateByParam(query);
	}

}
